package main

import (
	"context"
	"fmt"
	"sieve_agent_server/lib/server"
)

func GetBusinessType(ctx *server.Context) interface{} {
	var (
		querySQL = fmt.Sprintf(`
SELECT
  business_type_configuration.id AS id,
  business_type_configuration.name AS name,
  business_type_configuration.desc AS %v,
  business_type_configuration.unit_price AS unit_price,
  business_type_configuration.status AS status,
  business_type_configuration.role_id AS role_id,
  business_type_configuration.creator AS creator,
  user.account AS creator_name,
  business_type_configuration.create_at AS create_at,
  business_type_configuration.updated_at AS updated_at
FROM business_type_configuration
  LEFT JOIN user
    ON user.id = business_type_configuration.creator
    AND user.deleted = 0
WHERE business_type_configuration.deleted = 0
AND business_type_configuration.role_id = %v
GROUP BY business_type_configuration.name
ORDER BY business_type_configuration.create_at DESC
`, "`desc`", adminRole.ID)
	)

	resp, err := db.QueryRaw(context.Background(), querySQL, QueryBizTy{})
	if err != nil {
		return err
	}

	ret := map[string]interface{}{
		"list":  resp,
		"total": len(resp),
	}

	return ret
}

func GetSieveSourceConfig(ctx *server.Context) interface{} {
	var (
		querySQL = fmt.Sprintf(`
SELECT
  *
FROM sieve_source_config
WHERE deleted = 0
`)
	)

	resp, err := db.QueryRaw(context.Background(), querySQL, SieveSourceConfig{})
	if err != nil {
		return err
	}

	ret := map[string]interface{}{
		"list":  resp,
		"total": len(resp),
	}

	return ret
}
